﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Document_Management
{
    public partial class frmImageMnt : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        int width, height;
        int iflag = 0;
        string str = null;
        bool b圖面Search, b圖面Insert, b圖面Edit, b圖面Del;
        public frmImageMnt(bool b圖面Search, bool b圖面Insert, bool b圖面Edit, bool b圖面Del)
        {
            InitializeComponent();
            this.b圖面Search = b圖面Search;
            this.b圖面Insert = b圖面Insert;
            this.b圖面Edit = b圖面Edit;
            this.b圖面Del = b圖面Del;
        }

        //Load Frm iMageMnt
        private void frmImageMnt_Load(object sender, EventArgs e)
        {
            if (!b圖面Insert)
            {
                btnaddnews.Enabled = false;
                btnAddnew.Enabled = false;
                btnAdd製造商.Enabled = false;
                btnAdd建檔人.Enabled = false;
                btnAdd借用人.Enabled = false;
            }
            if (!b圖面Search)
            {
                btnSearch.Enabled = false;
            }
            if (!b圖面Edit)
            {
                btnEdit.Enabled = false;
                btnUpdate.Enabled = false;
                btnEdit製造商.Enabled = false;
                btnEdit建檔人.Enabled = false;
                btnEdit借用人.Enabled = false;
            }
            if (!b圖面Del)
            {
                btndelete.Enabled = false;
                btnDelete廠區.Enabled = false;
                btnDelete製造商.Enabled = false;
                btnDelete建檔人.Enabled = false;
                btnDelete借用人.Enabled = false;
            }
            LoadAll();
            LoadAll廠區();
            LoadAll製造商();
            LoadAll建檔人();
            LoadAll借用人();
            loadcombobox();
            ResetText圖面();
            ResetText廠區();
            ResetText建檔人();
            ResetText借用人();
        }

        //Reset all Text 圖面
        public void ResetText圖面()
        {
            txt圖號1.ResetText();
            txt廠區.ResetText();
            txt圖面尺寸1.ResetText();
            cb零件圖_組立圖1.ResetText();
            txt版次1.ResetText();
            txt製造商.ResetText();
            txt附件上傳1.ResetText();
            txt建檔人.ResetText();
            txt借用人.ResetText();

            txt廠區1.Text = "";
            txt圖號.Text = "";
            txt圖面尺寸.Text = "";
            cb零件圖_組立圖.Text = "零件圖/組立圖";
            txt版次.Text = "";
            txt製造商1.Text = "";
            txt附件上傳.Text = "";
            txt建檔人1.Text = "";
            txt借用人1.Text = "";
        }

        //reset all text 借用人
        public void ResetText借用人()
        {           
            txt借用人代號.ResetText();
            txt借用人姓名.ResetText();
            rtb借用人備註.ResetText();
        }

        //reset all text 廠區
        public void ResetText廠區()
        {
            txt廠區代號.ResetText();
            txt廠區名稱.ResetText();
            rtxt圖面備註.ResetText();
            //txt廠區代號.Text = "";
            //txt廠區名稱.Text = "";
            //rtxt備註.Text = "";
        }

        //reset all text 製造商
        public void ResetText製造商()
        {
            txt製造商代號.ResetText();
            txt製造商名稱.ResetText();
            rtxt製造商備註.ResetText();
        }
        
        //reset all text 建檔人
        public void ResetText建檔人()
        {
            txt建檔人代號.ResetText();
            txt建檔人姓名.ResetText();
            rtb建檔人備註.ResetText();
        }

        //LoadAll圖面
        public void LoadAll()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {

                con.Open();
                //string sql = "Select * From 圖面";
                string sql = "SELECT 圖面.圖號, 圖面廠區.廠區代號, 圖面.圖面尺寸, 圖面製造商.製造商代號, 圖面.版次, 圖面.圖別, 圖面.附件路徑, 圖面建檔人.建檔人代號, 圖面借用人.借用人代號 FROM 圖面 INNER JOIN 圖面廠區 ON 圖面.廠區代號 = 圖面廠區.廠區代號 INNER JOIN 圖面製造商 ON 圖面.製造商代號 = 圖面製造商.製造商代號 INNER JOIN 圖面建檔人 ON 圖面.建檔人代號 = 圖面建檔人.建檔人代號 INNER JOIN 圖面借用人 ON 圖面.借用人代號 = 圖面借用人.借用人代號";
                //string sql = "SELECT 圖面.圖號,圖面廠區.廠區代號, 圖面廠區.廠區名稱, 圖面.圖面尺寸,圖面製造商.製造商代號, 圖面製造商.製造商名稱, 圖面.版次, 圖面.圖別, 圖面.附件路徑, 圖面建檔人.建檔人代號, 圖面建檔人.姓名, 圖面借用人.借用人代號, 圖面借用人.姓名 FROM 圖面 INNER JOIN 圖面廠區 ON 圖面.廠區代號 = 圖面廠區.廠區代號 INNER JOIN 圖面製造商 ON 圖面.製造商代號 = 圖面製造商.製造商代號 INNER JOIN 圖面建檔人 ON 圖面.建檔人代號 = 圖面建檔人.建檔人代號 INNER JOIN 圖面借用人 ON 圖面.借用人代號 = 圖面借用人.借用人代號";

                SqlDataAdapter ad = new SqlDataAdapter(sql, con);
                DataTable dt = new DataTable();
                ad.Fill(dt);
                dataGridstockvehicles.DataSource = dt;

                PublicClass.setRowNumber(dataGridstockvehicles);
                dataGridstockvehicles.Columns[0].Width = 70;
                //dataGridstockvehicles.Columns[1].Visible = false;
                //dataGridstockvehicles.Columns[4].Visible = false;
                //dataGridstockvehicles.Columns[9].Visible = false;
                //dataGridstockvehicles.Columns[11].Visible = false;

                //dataGridstockvehicles.Columns[10].HeaderText = "建檔人名稱";
                //dataGridstockvehicles.Columns[12].HeaderText = "借用人名稱"
                //dataGridstockvehicles.Columns[7].HeaderText = "建檔人名稱";
                //dataGridstockvehicles.Columns[8].HeaderText = "借用人名稱";
                //dataGridstockvehicles.Columns[4].Width = 60;
                //dataGridstockvehicles.Columns[5].Width = 40;
                //dataGridstockvehicles.Columns[6].Width = 100;
                dataGridstockvehicles.Columns[7].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dataGridstockvehicles.Columns[8].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                //dataGridstockvehicles.Columns[4].Width = 70;

                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
            width = this.Size.Width;
            height = this.Size.Height;
            gBAddNew.Visible = false;
            this.Size = new System.Drawing.Size(this.Size.Width, this.Size.Height - gBAddNew.Height);
            //this.DesktopLocation = new Point(0, 0);
        }

        //LoadAll廠區
        public void LoadAll廠區()
        {
            SqlConnection con = new SqlConnection(connectionString);
            string sql = "Select * From 圖面廠區";
            SqlDataAdapter ad = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dataGridView圖面廠區.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView圖面廠區.DataSource = dt;
            PublicClass.setRowNumber(dataGridView圖面廠區);
        }

        //LoadAll製造商
        public void LoadAll製造商()
        {
            SqlConnection con = new SqlConnection(connectionString);
            string sql = "Select * From 圖面製造商";
            SqlDataAdapter ad = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dataGridView圖面製造商.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView圖面製造商.DataSource = dt;
            PublicClass.setRowNumber(dataGridView圖面製造商);
        }

        //LoadAll建檔人
        public void LoadAll建檔人()
        {
            SqlConnection con = new SqlConnection(connectionString);
            string sql = "Select * From 圖面建檔人";
            SqlDataAdapter ad = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dataGridView建檔人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView建檔人.DataSource = dt;
            PublicClass.setRowNumber(dataGridView建檔人);
        }

        //LoadAll借用人
        public void LoadAll借用人()
        {
            SqlConnection con = new SqlConnection(connectionString);
            string sql = "Select * From 圖面借用人";
            SqlDataAdapter ad = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dataGridView借用人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView借用人.DataSource = dt;
            PublicClass.setRowNumber(dataGridView借用人);

        }

        //LoadCombobox     
        public void loadcombobox()
        {
            ////Load CmbTable-圖面廠區
            //SqlConnection con = new SqlConnection(connectionString);
            //string sql = "select * from 圖面廠區";
            //SqlDataAdapter ad = new SqlDataAdapter(sql, con);
            //DataTable dt = new DataTable();
            //ad.Fill(dt);
            //txt廠區1.DataSource = dt;
            //txt廠區1.DisplayMember = "廠區名稱";
            ////cb廠區.DisplayMember = "廠區代號";
            //txt廠區1.ValueMember = "廠區代號";

            ////Load CmbTable-圖面製造商
            //string sql4 = "select * from 圖面製造商";
            //SqlDataAdapter ad4 = new SqlDataAdapter(sql4, con);
            //DataTable dt4 = new DataTable();
            //ad4.Fill(dt4);
            //cb製造商.DataSource = dt4;
            //cb製造商.DisplayMember = "製造商名稱";
            ////cb製造商.DisplayMember = "製造商代號";
            //cb製造商.ValueMember = "製造商代號";

            ////Load CmbTable-圖面借用人
            //string sql2 = "select * from 圖面借用人";
            //SqlDataAdapter ad2 = new SqlDataAdapter(sql2, con);
            //DataTable dt2 = new DataTable();
            //ad2.Fill(dt2);
            //cb借用人.DataSource = dt2;
            //cb借用人.DisplayMember = "姓名";
            ////cb借用人.DisplayMember = "借用人代號";
            //cb借用人.ValueMember = "借用人代號";

            ////Load CmbTable-圖面建檔人
            //string sql3 = "select * from 圖面建檔人";
            //SqlDataAdapter ad3 = new SqlDataAdapter(sql3, con);
            //DataTable dt3 = new DataTable();
            //ad3.Fill(dt3);
            //cb建檔人.DataSource = dt3;
            //cb建檔人.DisplayMember = "姓名";
            ////cb建檔人.DisplayMember = "建檔人代號";
            //cb建檔人.ValueMember = "建檔人代號";

            //
            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add("0", "零件圖");
            data.Add("1", "組立圖");
            cb零件圖_組立圖1.DataSource = new BindingSource(data, null);
            cb零件圖_組立圖1.DisplayMember = "Value";
            cb零件圖_組立圖1.ValueMember = "Key";
            cb零件圖_組立圖1.DropDownStyle = ComboBoxStyle.DropDownList;

            //
            Dictionary<string, string> data2 = new Dictionary<string, string>();
            data2.Add("0", "零件圖");
            data2.Add("1", "組立圖");
            cb零件圖_組立圖.DataSource = new BindingSource(data, null);
            cb零件圖_組立圖.DisplayMember = "Value";
            cb零件圖_組立圖.ValueMember = "Key";
            cb零件圖_組立圖.DropDownStyle = ComboBoxStyle.DropDownList;
        }
        
        //==========================================================================================================//
        //Code 圖面
        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Test","hh");
            Enabled_Disibled_delete();
            hiden();
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string sp = "sp_圖面_尋找";
                SqlCommand cmd = new SqlCommand(sp, con);
                cmd.CommandType = CommandType.StoredProcedure;
                
                if (txt圖號1.Text != "") cmd.Parameters.AddWithValue("@圖號", txt圖號1.Text);
                if (txt廠區.Text.ToString() != "") cmd.Parameters.AddWithValue("@廠區代號", txt廠區.Text);
                if (txt圖面尺寸1.Text != "") cmd.Parameters.AddWithValue("@圖面尺寸", txt圖面尺寸1.Text);
                if (txt製造商.Text.ToString() != "") cmd.Parameters.AddWithValue("@製造商代號", txt製造商.Text);
                if (txt版次1.Text != "") cmd.Parameters.AddWithValue("@版次", txt版次1.Text);
                //if (cb零件圖_組立圖1.Text != "") cmd.Parameters.AddWithValue("@圖別", (bool)(cb零件圖_組立圖1.Text != "零件圖"));
                if (cb零件圖_組立圖1.Text != "") cmd.Parameters.AddWithValue("@圖別", cb零件圖_組立圖1.Text);//cmd.Parameters.AddWithValue("@圖別", (bool)cb零件圖_組立圖1.SelectedValue);
                if (txt附件上傳.Text != "") cmd.Parameters.AddWithValue("@附件路徑", txt附件上傳1.Text);
                if (txt建檔人.Text.ToString() != "") cmd.Parameters.AddWithValue("@建檔人代號", txt建檔人.Text);
                if (txt借用人.Text.ToString() != "") cmd.Parameters.AddWithValue("@借用人代號", txt借用人.Text);
                SqlDataReader sqlreader = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                //dt.Columns.Add("項次");//add new 項次 column
                dt.Columns.Add("圖號");
                dt.Columns.Add("廠區代號");
                dt.Columns.Add("圖面尺寸");
                dt.Columns.Add("製造商代號");
                dt.Columns.Add("版次");
                dt.Columns.Add("圖別");
                dt.Columns.Add("附件路徑");
                dt.Columns.Add("建檔人代號");
                dt.Columns.Add("借用人代號");
                //int k=1;
                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    //dtrows["項次"]=k.ToString();
                    dtrows["圖號"] = sqlreader["圖號"];
                    dtrows["廠區代號"] = sqlreader["廠區代號"];
                    dtrows["圖面尺寸"] = sqlreader["圖面尺寸"];
                    dtrows["製造商代號"] = sqlreader["製造商代號"];
                    dtrows["版次"] = sqlreader["版次"];
                    dtrows["圖別"] = sqlreader["圖別"];
                    dtrows["附件路徑"] = sqlreader["附件路徑"];
                    dtrows["建檔人代號"] = sqlreader["建檔人代號"];
                    dtrows["借用人代號"] = sqlreader["借用人代號"];
                    dt.Rows.Add(dtrows);
                    //k++;
                }

                //change column width
                //dataGridstockvehicles.Columns[0].Width = 30;
                dataGridstockvehicles.DataSource = dt;
                PublicClass.setRowNumber(dataGridstockvehicles);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    con.Close();
                }
        }

        private void btnResetAllText圖面_Click(object sender, EventArgs e)
        {
            Enabled_Disibled_delete();
            ResetText圖面();
            hiden();
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                ResetText圖面();
                con.Open();
                string sp = "sp_圖面_尋找";
                SqlCommand cmd = new SqlCommand(sp, con);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataReader sqlreader = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Columns.Add("圖號");
                dt.Columns.Add("廠區代號");
                dt.Columns.Add("圖面尺寸");
                dt.Columns.Add("製造商代號");
                dt.Columns.Add("版次");
                dt.Columns.Add("圖別");
                dt.Columns.Add("附件路徑");
                dt.Columns.Add("建檔人代號");
                dt.Columns.Add("借用人代號");
                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["圖號"] = sqlreader["圖號"];
                    dtrows["廠區代號"] = sqlreader["廠區代號"];
                    dtrows["圖面尺寸"] = sqlreader["圖面尺寸"];
                    dtrows["製造商代號"] = sqlreader["製造商代號"];
                    dtrows["版次"] = sqlreader["版次"];
                    dtrows["圖別"] = sqlreader["圖別"];
                    dtrows["附件路徑"] = sqlreader["附件路徑"];
                    dtrows["建檔人代號"] = sqlreader["建檔人代號"];
                    dtrows["借用人代號"] = sqlreader["借用人代號"];
                    dt.Rows.Add(dtrows);
                }

                dataGridstockvehicles.DataSource = dt;
                PublicClass.setRowNumber(dataGridstockvehicles);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            gBAddNew.Visible = false;
            this.Size = new Size(width, height - gBAddNew.Height);
            btnaddnews.Enabled = true;
            btndelete.Enabled = true;
            btnEdit.Enabled = true;
            
        }

        public void hiden()
        {
            gBAddNew.Visible = false;
            this.Size = new Size(width, height - gBAddNew.Height);
            btnaddnews.Enabled = true;
            btndelete.Enabled = true;
            btnEdit.Enabled = true;
        }

        private void btnaddproduct_Click(object sender, EventArgs e)
        {
            iflag = 1;
            btninsertupdatedelete.Text = "實現";
            ResetText圖面();
            this.Size = new System.Drawing.Size(width, height);
            gBAddNew.Visible = true;
            Enabled_Disibled_Addnews();
        }

        private void Enabled_Disibled_Addnews()
        {
            btnaddnews.Enabled = true;
            btnEdit.Enabled = true;
            btndelete.Enabled = true;
            btnchose.Enabled = true;
            txt廠區1.Enabled = true;
            txt圖號.Enabled = true;
            txt圖面尺寸.Enabled = true;
            cb零件圖_組立圖.Enabled = true;
            txt版次.Enabled = true;
            txt製造商1.Enabled = true;
            txt附件上傳.Enabled = true;
            txt建檔人1.Enabled = true;
            txt借用人1.Enabled = true;
        }

        private void btnEditproduct_Click(object sender, EventArgs e)
        {
            iflag = 2;
            btninsertupdatedelete.Text = "更新";
            this.Size = new System.Drawing.Size(width, height);
            gBAddNew.Visible = true;
            Enabled_Disibled_Edit();
        }

        private void Enabled_Disibled_Edit()
        {
            //btnaddnews.Enabled = false;
            //btnEdit.Enabled = true;
            //btndelete.Enabled = false;
            //btnchose.Enabled = true;
            txt廠區1.Enabled = true;
            txt圖號.Enabled = true;
            txt圖面尺寸.Enabled = true;
            cb零件圖_組立圖.Enabled = true;
            txt版次.Enabled = true;
            txt製造商1.Enabled = true;
            txt附件上傳.Enabled = true;
            txt建檔人1.Enabled = true;
            txt借用人1.Enabled = true;
            
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            iflag = 3;
            btninsertupdatedelete.Text = " 實現";
            this.Size = new System.Drawing.Size(width, height);
            gBAddNew.Visible = true;
            Enabled_Disibled_delete();
        }

        private void Enabled_Disibled_delete()
        {
            //btndelete.Enabled = true;
            //btnaddnews.Enabled = false;
            //btnEdit.Enabled = false;
            //btnchose.Enabled = false;
            txt廠區1.Enabled = false;
            txt圖號.Enabled = false;
            txt圖面尺寸.Enabled = false;
            cb零件圖_組立圖.Enabled = false;
            txt版次.Enabled = false;
            txt製造商1.Enabled = false;
            txt附件上傳.Enabled = false;
            txt建檔人1.Enabled = false;
            txt借用人1.Enabled = false;
            
        }

        private void btninsertupdatedelete_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            
                if (iflag == 1)//insert
                {
                try
                    {
                        //string fileToCopy = "E:\\Src\\a.docx";
                        //string destinationDirectory = "c:\\workspace\\";

                        //File.Copy(fileToCopy, destinationDirectory + Path.GetFileName(fileToCopy));
                    if (txt廠區1.Text == "")
                    {
                        MessageBox.Show("請您輸入廠區", "錯誤", MessageBoxButtons.OK);
                        txt廠區1.Focus();
                        return;
                    }
                    if (lbName廠區.Text == "")
                    {
                        MessageBox.Show("廠區代號不正確", "錯誤", MessageBoxButtons.OK);
                        txt廠區1.Focus();
                        return;
                    }
                    if (txt圖號.Text == "")
                    {
                        MessageBox.Show("請您輸入圖號代碼", "錯誤", MessageBoxButtons.OK);
                        txt圖號.Focus();
                        return;
                    }


                    if (txt圖面尺寸.Text == "")
                    {
                        MessageBox.Show("請您輸入圖面尺寸", "錯誤", MessageBoxButtons.OK);
                        txt圖面尺寸.Focus();
                        return;
                    }
                    if (txt製造商1.Text == "")
                    {
                        MessageBox.Show("請您輸入安製造商代碼", "錯誤", MessageBoxButtons.OK);
                        txt製造商1.Focus();
                        return;
                    }

                    if (lbName製造商.Text=="")
                    {
                        MessageBox.Show("造商代號不正確", "錯誤", MessageBoxButtons.OK);
                        txt製造商1.Focus();
                        return;
                    }

                    if (txt版次.Text == "")
                    {
                        MessageBox.Show("請您輸入版次圖面", "錯誤", MessageBoxButtons.OK);
                        txt版次.Focus();
                        return;
                    }

                    if (cb零件圖_組立圖.Text == "")
                    {
                        MessageBox.Show("請您挑選零件圖還是組立圖", "錯誤", MessageBoxButtons.OK);
                        cb零件圖_組立圖.Focus();
                        return;
                    }

                    //if (txt附件上傳.Text == "")
                    //{
                    //    MessageBox.Show("請您挑選供應商", "錯誤", MessageBoxButtons.OK);
                    //    txt附件上傳.Focus();
                    //    return;
                    //}

                    if (txt建檔人1.Text == "")
                    {
                        MessageBox.Show("請您輸入建檔人", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txt建檔人1.Focus();
                        return;
                    }

                    if (lbName建檔人.Text=="")
                    {
                        MessageBox.Show("建檔人代號不正確", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txt建檔人1.Focus();
                        return;
                    }

                    if (txt借用人1.Text == "")
                    {
                        MessageBox.Show("請您輸入借用人", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txt借用人1.Focus();
                        return;
                    }

                    if (lbName借用人.Text=="")
                    {
                        MessageBox.Show("借用人代號不正確", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txt建檔人1.Focus();
                        return;
                    }
                    //bat dau insert vao database

                    con.Open();
                    string sp_insert = "sp_圖面_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@圖號", txt圖號.Text);
                    cmd.Parameters.AddWithValue("@廠區代號", txt廠區1.Text);
                    cmd.Parameters.AddWithValue("@圖面尺寸", txt圖面尺寸.Text);
                    cmd.Parameters.AddWithValue("@製造商代號", txt製造商1.Text);
                    cmd.Parameters.AddWithValue("@版次", txt版次.Text);
                    cmd.Parameters.AddWithValue("@圖別", cb零件圖_組立圖.Text);
                    //cmd.Parameters.AddWithValue("@圖別", (bool)(cb零件圖_組立圖.Text != "零件圖"));
                    //cmd.Parameters.AddWithValue("@圖別", (KeyValuePair<string, string>)cb零件圖_組立圖.SelectedValue.); //(.key de la gia tri,. value de lay text)
                    cmd.Parameters.AddWithValue("@附件路徑", txt附件上傳.Text);
                    cmd.Parameters.AddWithValue("@建檔人代號", txt建檔人1.Text);
                    cmd.Parameters.AddWithValue("@借用人代號", txt借用人1.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("新增成功");
                    LoadAll();
                    this.Size = new System.Drawing.Size(width, height);
                    gBAddNew.Visible = true;
                    //reset all text
                    ResetText圖面();
                    hiden();
                }
                catch(Exception ex) 
                {
                    con.Close();
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
            if (iflag == 2)//update
            {
                //SqlConnection con = new SqlConnection(connectionString);
                try
                {
                    // string sp_update = "update 圖面廠區 set 廠區代號=N'" + txt廠區代號.Text + "',廠區名稱=N'" + txt廠區名稱.Text + "' where 備註=N'" + rtxt備註.Text + "'";
                    if (txt圖號.Text == "") { MessageBox.Show("請您輸入圖號更新");}
                    else
                    {
                        con.Open();
                        SqlCommand cmd2;
                        String sql = "SELECT COUNT(*) FROM 圖面 WHERE 圖號=N'" + str + "'";
                        cmd2 = new SqlCommand(sql, con);
                        if ((int)cmd2.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_圖面_更新";
                            cmd2 = new SqlCommand(sp_update, con);
                            cmd2.CommandType = CommandType.StoredProcedure;
                            //con.Open();
                            cmd2.Parameters.AddWithValue("@圖號", str);
                            cmd2.Parameters.AddWithValue("@圖號2", txt圖號.Text);
                            cmd2.Parameters.AddWithValue("@廠區代號", txt廠區1.Text);
                            cmd2.Parameters.AddWithValue("@圖面尺寸", txt圖面尺寸.Text);
                            cmd2.Parameters.AddWithValue("@製造商代號", txt製造商1.Text);
                            cmd2.Parameters.AddWithValue("@版次", txt版次.Text);
                            //cmd2.Parameters.AddWithValue("@圖別", (bool)(cb零件圖_組立圖.Text != "零件圖"));
                            cmd2.Parameters.AddWithValue("@圖別", cb零件圖_組立圖.Text);
                            //cmd2.Parameters.AddWithValue("@圖別", cb零件圖_組立圖.Text.ToString());
                            cmd2.Parameters.AddWithValue("@附件路徑", txt附件上傳.Text);
                            cmd2.Parameters.AddWithValue("@建檔人代號", txt建檔人1.Text);
                            cmd2.Parameters.AddWithValue("@借用人代號", txt借用人1.Text);

                            cmd2.ExecuteNonQuery();
                            MessageBox.Show("更新成功");
                            LoadAll();
                            this.Size = new System.Drawing.Size(width, height);
                            gBAddNew.Visible = true;
                            con.Close();
                            hiden();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt圖號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    LoadAll();
                    Close();
                    return;
                }            
            }

            if (iflag == 3)//delete
            {
                try
                {
                    if (txt圖號.Text == "") { MessageBox.Show("請您輸入圖號刪除"); }
                    else
                    {
                        
                        DialogResult answer = MessageBox.Show("您確定要出入這個產品碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            con.Open();
                            string sp_delete = "sp_圖面_刪除";
                            //string sp_delete = "delete from 圖面 where 圖號='" + txt圖號.Text + "'";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            //SqlCommand sqldelete = new SqlCommand();
                            //sqldelete.Connection = con;
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            //sqldelete.CommandText = sp_delete;
                            sqldelete.Parameters.AddWithValue("@圖號", txt圖號.Text);
                            MessageBox.Show("刪除成功");
                            sqldelete.ExecuteNonQuery();

                            LoadAll();
                            this.Size = new System.Drawing.Size(width, height);
                            gBAddNew.Visible = true;
                            hiden();
                            con.Close();
                            return;
                        }
                        else
                        {
                            LoadAll();
                            return;
                        }
                        
                    }
                }
                catch (Exception ex)
                {
                    con.Close();
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }

        }

        private void dataGridstockvehicles_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                //int r = e.RowIndex; 
                //txt圖號.Text = dataGridstockvehicles.Rows[r].Cells[0].Value.ToString();
                //txt廠區1.Text = dataGridstockvehicles.Rows[r].Cells[1].Value.ToString();
                //txt圖面尺寸.Text = dataGridstockvehicles.Rows[r].Cells[3].Value.ToString();
                //txt製造商1.Text = dataGridstockvehicles.Rows[r].Cells[4].Value.ToString();
                //txt版次.Text = dataGridstockvehicles.Rows[r].Cells[6].Value.ToString();
                //cb零件圖_組立圖.Text = dataGridstockvehicles.Rows[r].Cells[7].Value.ToString();
                //txt附件上傳.Text = dataGridstockvehicles.Rows[r].Cells[8].Value.ToString();
                //txt建檔人1.Text = dataGridstockvehicles.Rows[r].Cells[9].Value.ToString();
                //txt借用人1.Text = dataGridstockvehicles.Rows[r].Cells[11].Value.ToString();
                int r = e.RowIndex;
                str = dataGridstockvehicles.Rows[r].Cells[0].Value.ToString();
                txt圖號.Text = dataGridstockvehicles.Rows[r].Cells[0].Value.ToString();
                txt廠區1.Text = dataGridstockvehicles.Rows[r].Cells[1].Value.ToString();
                txt圖面尺寸.Text = dataGridstockvehicles.Rows[r].Cells[2].Value.ToString();
                txt製造商1.Text = dataGridstockvehicles.Rows[r].Cells[3].Value.ToString();
                txt版次.Text = dataGridstockvehicles.Rows[r].Cells[4].Value.ToString();
                cb零件圖_組立圖.Text = dataGridstockvehicles.Rows[r].Cells[5].Value.ToString();
                txt附件上傳.Text = dataGridstockvehicles.Rows[r].Cells[6].Value.ToString();
                txt建檔人1.Text = dataGridstockvehicles.Rows[r].Cells[7].Value.ToString();
                txt借用人1.Text = dataGridstockvehicles.Rows[r].Cells[8].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void dataGridView借用人_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //PublicClass.setRowNumber(dataGridView借用人);
        }

        private void dataGridstockvehicles_ColumnHeaderMouseClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGridstockvehicles);
        }

        private void dataGridstockvehicles_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGridstockvehicles);
        }

        private void dataGridView借用人_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGridView借用人);
        }

        public void DocXML()
        {
            string path = "";
            string path2 = "";
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml("E:\\books.xml");
                string temp = "";
                string temp2 = "";
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    temp += dr["UrlServer"].ToString() + "\n";
                    temp2 += dr["UrlClient"].ToString() + "\n";
                }
                temp = temp.Substring(0, temp.Length - 1);
                temp2 = temp2.Substring(0, temp2.Length - 1);
                path = temp;
                path2 = temp2;
            }
            catch { }
        }

        public void copyfile()
        {
            //string fileToCopy = "E:\\Src\\a.docx";
            //string fileToCopy = "";
            //string destinationDirectory = "c:\\workspace\\";
            //fileToCopy = txt附件上傳.Text;
            //File.Copy(fileToCopy, destinationDirectory + Path.GetFileName(fileToCopy));
        }

        //======CopyToFolder Login User=========
        //[DllImport("advapi32.DLL", SetLastError = true)]
        //public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

        //static void CopyToSharedFolder()
        //{
        //    IntPtr admin_token = default(IntPtr);
        //    WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
        //    WindowsIdentity wid_admin = null;
        //    WindowsImpersonationContext wic = null;
        //    try
        //    {
        //        //if (LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, ref admin_token) != 0)
        //        if (LogonUser("username", "domain", "password", 9, 0, ref admin_token) != 0)
        //        {
        //            wid_admin = new WindowsIdentity(admin_token);
        //            wic = wid_admin.Impersonate();
        //            //System.IO.File.Copy("C:\\right.bmp", "\\\\157.60.113.28\\testnew\\right.bmp", true);
        //            System.IO.File.Copy(@"D:\ram\Test.txt", @"\\10.245.66.50\pdc\Test.txt", true);
        //        }
        //    }
        //    catch (System.Exception se)
        //    {
        //        int ret = Marshal.GetLastWin32Error();
        //    }
        //    finally
        //    {
        //        if (wic != null)
        //        {
        //            wic.Undo();
        //        }
        //    }
        //}
        //===========================================================

        private void btnchose_Click(object sender, EventArgs e)
        {
            //IntPtr admin_token = default(IntPtr);
            //WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
            //WindowsIdentity wid_admin = null;
            //WindowsImpersonationContext wic = null;
            string Rrl = "";
            string Rrl2 = "";
            string temp = "";
            string temp2 = "";
            string fileName;
            string path = "";
            string fileToCopy = "";
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "開啟檔案(Attach customer proposal document)";
            //fDialog.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
            fDialog.Filter = "檔案當 (*.txt, *.doc, *.docx, *.xls, *.xlsx, *.ppt, *.pptx, *.pdf) | *.txt; *.doc; *.docx; *.xlsx;*.xls; *.ppt; *.pptx; *.pdf | All Files|*.*";
            fDialog.InitialDirectory = Application.StartupPath + @"Documents\\";
            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                fileName = System.IO.Path.GetFileName(fDialog.FileName);
                path = Path.GetDirectoryName(fDialog.FileName);
                txt附件上傳.Text = path + "\\" + fileName;
                //copyfile(); 
                try
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml("C:\\config\\config圖面.xml");
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        temp += dr["UrlServer"].ToString() + "\n";
                        temp2 += dr["UrlClient"].ToString() + "\n";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                    temp2 = temp2.Substring(0, temp2.Length - 1);
                    Rrl = temp;
                    Rrl2 = temp2;
                    //==================================================
                    //string destinationDirectory = @"C:\workspace\";
                    string destinationDirectory = @"" + Rrl + "";
                    fileToCopy = txt附件上傳.Text;
                    File.Copy(fileToCopy, destinationDirectory + Path.GetFileName(fileToCopy), true);
                    txt附件上傳.Text = destinationDirectory + "\\" + fileName;
                }
                catch { }
        
                //if (LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, ref admin_token) != 0)
                //if (LogonUser("username", "domain", "password", 9, 0, ref admin_token) != 0)
                //{
                //    wid_admin = new WindowsIdentity(admin_token);
                //    wic = wid_admin.Impersonate();
                    //System.IO.File.Copy("C:\\right.bmp", "\\\\157.60.113.28\\testnew\\right.bmp", true);
                    //System.IO.File.Copy(@"D:\ram\Test.txt", @"\\10.245.66.50\pdc\Test.txt", true);
                    //string destinationDirectory = @"\\10.198.163.99\c\";
                    //fileToCopy = txt附件上傳.Text;
                    //File.Copy(fileToCopy, destinationDirectory + Path.GetFileName(fileToCopy),true);
                    //txt附件上傳.Text = destinationDirectory + "\\" + fileName;
                //}    
            }
        }

        private void dataGridstockvehicles_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string Rrl = "";
            string Rrl2 = "";
            string temp = "";
            //string temp2 = "";
            try
            {

                //OpenFileDialog fDialog = new OpenFileDialog();
                //fDialog.Title = "開啟檔案(Attach customer proposal document)";

                DialogResult result = new DialogResult();
                PublicClass.setRowNumber(dataGridstockvehicles);
                if (dataGridstockvehicles.CurrentCell.ColumnIndex.Equals(6))
                {
                    if (dataGridstockvehicles.CurrentCell != null && dataGridstockvehicles.CurrentCell.Value != null)
                    {
                        string rooturl = dataGridstockvehicles.CurrentCell.Value.ToString() ;
                        result = MessageBox.Show("你要不要存檔這個資料？" + "\n" + rooturl, "存檔圖面", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                        FolderBrowserDialog fDialogSave = new FolderBrowserDialog();
                        if (fDialogSave.ShowDialog() == DialogResult.OK)
                        {
                            string foldername = fDialogSave.SelectedPath;
                            DataSet ds = new DataSet();
                            ds.ReadXml("C:\\config\\config圖面.xml");
                            foreach (DataRow dr in ds.Tables[0].Rows)
                            {
                                temp += dr["UrlServer"].ToString() + "\n";
                                //temp2 += dr["UrlClient"].ToString() + "\n";
                            }
                            temp = temp.Substring(0, temp.Length - 1);
                            //temp2 = temp2.Substring(0, temp2.Length - 1);
                            Rrl = temp;
                            Rrl = Rrl + Path.GetFileName(dataGridstockvehicles.CurrentCell.Value.ToString());
                            //Rrl2 = temp2;
                            Rrl2 = foldername+"\\" + Path.GetFileName(dataGridstockvehicles.CurrentCell.Value.ToString());
                            File.Copy(Rrl, Rrl2, true);
                            MessageBox.Show("另存為成功");
                            //LoadAll();
                            return;
                            }
                        }
                        if (result == DialogResult.No)
                        {
                            //MessageBox.Show("還沒成功");
                            LoadAll();
                            return;
                        }
                    }
                }
            }
            catch { }
        }

        private void tabAll_SelectedIndexChanged(object sender, EventArgs e)
        {
            gBAddNew.Visible = false;
            this.Size = new Size(width, height - gBAddNew.Height);

            PublicClass.setRowNumber(dataGridstockvehicles);
            PublicClass.setRowNumber(dataGridView建檔人);
            PublicClass.setRowNumber(dataGridView圖面廠區);
            PublicClass.setRowNumber(dataGridView圖面製造商);
            PublicClass.setRowNumber(dataGridView借用人);

            //ResetText圖面();
            ResetText廠區();
            ResetText借用人();
            ResetText製造商();
            ResetText建檔人();
        }

        private void btn_廠區_查詢_Click(object sender, EventArgs e)
        {
            FrmSearchFactoryMnt frm = new FrmSearchFactoryMnt(txt廠區1);
            frm.ShowDialog();
        }

        private void btn製造商Search_Click(object sender, EventArgs e)
        {
            FrnSearchFtrfabricate frm = new FrnSearchFtrfabricate(txt製造商1);
            frm.ShowDialog();
        }

        private void btn建檔人Search_Click(object sender, EventArgs e)
        {
            FrmSearchDesignby frm = new FrmSearchDesignby(txt建檔人1);
            frm.ShowDialog();
        }

        private void btn借用人Search_Click(object sender, EventArgs e)
        {
            FrmSearchdesignUser frm = new FrmSearchdesignUser(txt借用人1);
            frm.ShowDialog();
        }
        //End Code 圖面

        //======================================================================================================//
        //Code 圖面廠區
        //dataGridView圖面廠區
        private void dataGridView圖面廠區_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int dong = e.RowIndex;
                str = dataGridView圖面廠區.Rows[dong].Cells[0].Value.ToString();
                txt廠區代號.Text = dataGridView圖面廠區.Rows[dong].Cells[0].Value.ToString();
                txt廠區名稱.Text = dataGridView圖面廠區.Rows[dong].Cells[1].Value.ToString();
                rtxt圖面備註.Text = dataGridView圖面廠區.Rows[dong].Cells[2].Value.ToString();
            }
            catch
            {

            }
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            ResetText廠區();
        }

        private void btnAddnew_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            //tien hanh insert
            try
            {
                if (txt廠區代號.Text == "")
                {
                    MessageBox.Show("請您輸入材編", "錯誤", MessageBoxButtons.OK);
                    txt廠區代號.Focus();
                    return;
                }

                if (txt廠區名稱.Text == "")
                {
                    MessageBox.Show("請您輸入品名", "錯誤", MessageBoxButtons.OK);
                    txt廠區名稱.Focus();
                    return;
                }
                //bat dau insert vao database
                con.Open();
                string sp_insert = "sp_圖面廠區_新增";
                SqlCommand cmd = new SqlCommand(sp_insert, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@廠區代號", txt廠區代號.Text);
                cmd.Parameters.AddWithValue("@廠區名稱", txt廠區名稱.Text);
                cmd.Parameters.AddWithValue("@備註", rtxt圖面備註.Text.ToString());
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增成功");
                //reset all text
                LoadAll廠區();
                ResetText廠區();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Close();
                return;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                if (txt廠區代號.Text == "") { MessageBox.Show("請您輸入廠區代號更新"); }
                else
                {
                    // string sp_update = "update 圖面廠區 set 廠區代號=N'" + txt廠區代號.Text + "',廠區名稱=N'" + txt廠區名稱.Text + "' where 備註=N'" + rtxt備註.Text + "'";
                    con.Open();
                    SqlCommand cmd3;
                    String sql = "SELECT COUNT(*) FROM 圖面廠區 WHERE 廠區代號=N'" + str + "'";
                    cmd3 = new SqlCommand(sql, con);
                    if ((int)cmd3.ExecuteScalar() > 0)
                    {
                        string sp_update = "sp_圖面廠區_更新2";
                        cmd3 = new SqlCommand(sp_update, con);
                        cmd3.CommandType = CommandType.StoredProcedure;
                        //con.Open();
                        cmd3.Parameters.AddWithValue("@廠區代號", str);
                        cmd3.Parameters.AddWithValue("@廠區代號2", txt廠區代號.Text);
                        cmd3.Parameters.AddWithValue("@廠區名稱", txt廠區名稱.Text);
                        cmd3.Parameters.AddWithValue("@備註", rtxt圖面備註.Text.ToString());

                        cmd3.ExecuteNonQuery();
                        MessageBox.Show("更新成功");
                        LoadAll廠區();
                        ResetText廠區();
                        con.Close();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt廠區代號.Text.ToString() + "' 不由來");
                        con.Close();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Close();
                return;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            //iflag=3, tien hanh delete
            try
            {
                if (txt廠區代號.Text == "") { MessageBox.Show("請您輸入廠區代號刪除"); }
                else
                {
                    
                    DialogResult answer = MessageBox.Show("您確定要出入這個產品碼", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        con.Open();
                        string sp_delete = "sp_圖面廠區_刪除";
                        //string sp_delete = "delete from 圖面廠區 where 廠區代號='" + txt廠區代號.Text + "'";
                        SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                        //SqlCommand sqldelete = new SqlCommand();
                        sqldelete.Connection = con;
                        sqldelete.CommandType = CommandType.StoredProcedure;
                        //sqldelete.CommandText = sp_delete;
                        sqldelete.Parameters.AddWithValue("@廠區代號", txt廠區代號.Text);
                        MessageBox.Show("刪除成功");
                        sqldelete.ExecuteNonQuery();

                        LoadAll廠區();
                        ResetText廠區();
                        return;
                        con.Close();
                    }
                    else
                    {
                        LoadAll廠區();
                        ResetText廠區();
                        return;
                    }
                    
                }
                
            }
            catch (Exception ex)
            {
                con.Close();
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dataGridView圖面廠區_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGridView圖面廠區);
        }

        //End Code 圖面廠區
        //==========================================================================================================//

        //Code 製造商
        private void dataGridView圖面製造商_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int dong = e.RowIndex;
                str = dataGridView圖面製造商.Rows[dong].Cells[0].Value.ToString();
                txt製造商代號.Text = dataGridView圖面製造商.Rows[dong].Cells[0].Value.ToString();
                txt製造商名稱.Text = dataGridView圖面製造商.Rows[dong].Cells[1].Value.ToString();
                rtxt製造商備註.Text = dataGridView圖面製造商.Rows[dong].Cells[2].Value.ToString();
            }
            catch
            {

            }
        }

        private void dataGridView圖面製造商_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGridView圖面製造商);
        }

        private void btn製造商_Click(object sender, EventArgs e)
        {
            ResetText製造商();
        }

        private void btnAdd製造商_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            //tien hanh insert
            try
            {
                if (txt製造商代號.Text == "")
                {
                    MessageBox.Show("請您輸入製造商代號", "錯誤", MessageBoxButtons.OK);
                    txt製造商代號.Focus();
                    return;
                }

                if (txt製造商名稱.Text == "")
                {
                    MessageBox.Show("請您輸入製造商名稱", "錯誤", MessageBoxButtons.OK);
                    txt製造商名稱.Focus();
                    return;
                }
                con.Open();
                string sp_insert = "sp_圖面製造商_新增";
                SqlCommand cmd = new SqlCommand(sp_insert, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@製造商代號", txt製造商代號.Text);
                cmd.Parameters.AddWithValue("@製造商名稱", txt製造商名稱.Text);
                cmd.Parameters.AddWithValue("@備註", rtxt製造商備註.Text.ToString());
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增成功");
                LoadAll製造商();
                ResetText製造商();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LoadAll製造商();
                Close();
                return;
            }
        }

        private void btnEdit製造商_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                if (txt製造商代號.Text == "") { MessageBox.Show("請您輸入廠區代號更新"); }
                else
                {
                    // string sp_update = "update 圖面廠區 set 廠區代號=N'" + txt廠區代號.Text + "',廠區名稱=N'" + txt廠區名稱.Text + "' where 備註=N'" + rtxt備註.Text + "'";
                    con.Open();
                    SqlCommand cmd3;
                    String sql = "SELECT COUNT(*) FROM 圖面製造商 WHERE 製造商代號=N'" + str + "'";
                    cmd3 = new SqlCommand(sql, con);
                    if ((int)cmd3.ExecuteScalar() > 0)
                    {
                        string sp_update = "sp_圖面製造商_更新";
                        cmd3 = new SqlCommand(sp_update, con);
                        cmd3.CommandType = CommandType.StoredProcedure;
                        //con.Open();
                        cmd3.Parameters.AddWithValue("@製造商代號", str);
                        cmd3.Parameters.AddWithValue("@製造商代號2", txt製造商代號.Text);
                        cmd3.Parameters.AddWithValue("@製造商名稱", txt製造商名稱.Text);
                        cmd3.Parameters.AddWithValue("@備註", rtxt製造商備註.Text.ToString());

                        cmd3.ExecuteNonQuery();
                        MessageBox.Show("更新成功");
                        LoadAll製造商();
                        ResetText製造商();
                        con.Close();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt製造商代號.Text.ToString() + "' 不由來");
                        con.Close();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LoadAll製造商();
                Close();
                return;
            }           
        }

        private void btnDelete製造商_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                if (txt製造商代號.Text == "") { MessageBox.Show("請您輸入廠區代號刪除"); }
                else
                {
                    
                    DialogResult answer = MessageBox.Show("您確定要出入這個產品碼", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        con.Open();
                        string sp_delete = "sp_圖面製造商_刪除";
                        //string sp_delete = "delete from 圖面製造商 where 製造商代號='" + txt製造商代號.Text + "'";
                        SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                        //SqlCommand sqldelete = new SqlCommand();
                        //sqldelete.Connection = con;
                        sqldelete.CommandType = CommandType.StoredProcedure;
                        //sqldelete.CommandText = sp_delete;
                        sqldelete.Parameters.AddWithValue("@製造商代號", txt製造商代號.Text);
                        MessageBox.Show("刪除成功");
                        sqldelete.ExecuteNonQuery();

                        LoadAll製造商();
                        ResetText製造商();
                        con.Close();
                        return;
                    }
                    else
                    {
                        LoadAll製造商();
                        return;
                    }
                    
                }
            }
            catch (Exception ex)
            {
                con.Close();
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        //End Code 製造商
        //==========================================================================================================//

        //Code 建檔人
        private void dataGridView建檔人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int dong = e.RowIndex;
                str = dataGridView建檔人.Rows[dong].Cells[0].Value.ToString();
                txt建檔人代號.Text = dataGridView建檔人.Rows[dong].Cells[0].Value.ToString();
                txt建檔人姓名.Text = dataGridView建檔人.Rows[dong].Cells[1].Value.ToString();
                rtb建檔人備註.Text = dataGridView建檔人.Rows[dong].Cells[2].Value.ToString();
            }
            catch
            {

            }
        }

        private void dataGridView建檔人_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGridView建檔人);
        }  

        private void btnClean建檔人_Click(object sender, EventArgs e)
        {
            ResetText建檔人();
        }

        private void btnAdd建檔人_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            //tien hanh insert
            try
            {
                if (txt建檔人代號.Text == "")
                {
                    MessageBox.Show("請您輸入建檔人代號", "錯誤", MessageBoxButtons.OK);
                    txt建檔人代號.Focus();
                    return;
                }

                if (txt建檔人姓名.Text == "")
                {
                    MessageBox.Show("請您輸入建檔人姓名", "錯誤", MessageBoxButtons.OK);
                    txt建檔人姓名.Focus();
                    return;
                }
                con.Open();
                string sp_insert = "sp_圖面建檔人_新增";
                SqlCommand cmd = new SqlCommand(sp_insert, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@建檔人代號", txt建檔人代號.Text);
                cmd.Parameters.AddWithValue("@姓名", txt建檔人姓名.Text);
                cmd.Parameters.AddWithValue("@備註", rtb建檔人備註.Text.ToString());
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增成功");
                con.Close();
                LoadAll建檔人();
                ResetText建檔人();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LoadAll建檔人();
                Close();
                return;
            }
        }

        private void btnEdit建檔人_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                if (txt建檔人代號.Text == "") { MessageBox.Show("請您輸入建檔人代號更新"); }
                else
                {
                    // string sp_update = "update 圖面廠區 set 廠區代號=N'" + txt廠區代號.Text + "',廠區名稱=N'" + txt廠區名稱.Text + "' where 備註=N'" + rtxt備註.Text + "'";
                    con.Open();
                    SqlCommand cmd3;
                    String sql = "SELECT COUNT(*) FROM 圖面建檔人 WHERE 建檔人代號=N'" + str + "'";
                    cmd3 = new SqlCommand(sql, con);
                    if ((int)cmd3.ExecuteScalar() > 0)
                    {
                        string sp_update = "sp_圖面建檔人_更新";
                        cmd3 = new SqlCommand(sp_update, con);
                        cmd3.CommandType = CommandType.StoredProcedure;
                        //con.Open();
                        cmd3.Parameters.AddWithValue("@建檔人代號", str);
                        cmd3.Parameters.AddWithValue("@建檔人代號2", txt建檔人代號.Text);
                        cmd3.Parameters.AddWithValue("@姓名", txt建檔人姓名.Text);
                        cmd3.Parameters.AddWithValue("@備註", rtb建檔人備註.Text.ToString());

                        cmd3.ExecuteNonQuery();
                        MessageBox.Show("更新成功");
                        LoadAll建檔人();
                        ResetText建檔人();
                        con.Close();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt建檔人代號.Text.ToString() + "' 不由來");
                        con.Close();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LoadAll建檔人();
                Close();
                return;
            }            
        }

        private void btnDelete建檔人_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                if (txt建檔人代號.Text == "") { MessageBox.Show("請您輸入建檔人代號刪除"); }
                else
                {
                    
                    DialogResult answer = MessageBox.Show("您確定要出入這個產品碼", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        con.Open();
                        string sp_delete = "sp_圖面建檔人_刪除";
                        //string sp_delete = "delete from 圖面建檔人 where 建檔人代號='" + txt建檔人代號.Text + "'";
                        SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                        //SqlCommand sqldelete = new SqlCommand();
                        //sqldelete.Connection = con;
                        sqldelete.CommandType = CommandType.StoredProcedure;
                        //sqldelete.CommandText = sp_delete;
                        sqldelete.Parameters.AddWithValue("@建檔人代號", txt建檔人代號.Text);
                        MessageBox.Show("刪除成功");
                        sqldelete.ExecuteNonQuery();
                        con.Close();
                        LoadAll建檔人();
                        ResetText建檔人();
                        return;
                    }
                    else
                    {
                        LoadAll建檔人();
                        return;
                    }
                    
                }
            }
            catch (Exception ex)
            {
                con.Close();
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        //End Code 借用人
        //==========================================================================================================//

        //Code 借用人
        private void dataGridView借用人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int dong = e.RowIndex;
                str = dataGridView借用人.Rows[dong].Cells[0].Value.ToString();
                txt借用人代號.Text = dataGridView借用人.Rows[dong].Cells[0].Value.ToString();
                txt借用人姓名.Text = dataGridView借用人.Rows[dong].Cells[1].Value.ToString();
                rtb借用人備註.Text = dataGridView借用人.Rows[dong].Cells[2].Value.ToString();
            }
            catch
            {
            }
        }

        private void btnClean借用人_Click(object sender, EventArgs e)
        {
            ResetText借用人();
        }

        private void btnAdd借用人_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            //tien hanh insert
            try
            {
                if (txt借用人代號.Text == "")
                {
                    MessageBox.Show("請您輸入借用人代號", "錯誤", MessageBoxButtons.OK);
                    txt借用人代號.Focus();
                    return;
                }

                if (txt借用人姓名.Text == "")
                {
                    MessageBox.Show("請您輸借用人姓名", "錯誤", MessageBoxButtons.OK);
                    txt借用人姓名.Focus();
                    return;
                }
                con.Open();
                string sp_insert = "sp_圖面借用人_新增";
                SqlCommand cmd = new SqlCommand(sp_insert, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@借用人代號", txt借用人代號.Text);
                cmd.Parameters.AddWithValue("@姓名", txt借用人姓名.Text);
                cmd.Parameters.AddWithValue("@備註", rtb借用人備註.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增成功");
                LoadAll借用人();
                ResetText借用人();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LoadAll借用人();
                Close();
                return;
            }
        }

        private void btnEdit借用人_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                if (txt借用人代號.Text == "") { MessageBox.Show("請您輸入借用人代號更新"); }
                else
                {
                    // string sp_update = "update 圖面廠區 set 廠區代號=N'" + txt廠區代號.Text + "',廠區名稱=N'" + txt廠區名稱.Text + "' where 備註=N'" + rtxt備註.Text + "'";
                    con.Open();
                    SqlCommand cmd3;
                    String sql = "SELECT COUNT(*) FROM 圖面借用人 WHERE 借用人代號=N'" + str + "'";
                    cmd3 = new SqlCommand(sql, con);
                    if ((int)cmd3.ExecuteScalar() > 0)
                    {
                        string sp_update = "sp_圖面借用人_更新";
                        cmd3 = new SqlCommand(sp_update, con);
                        cmd3.CommandType = CommandType.StoredProcedure;
                        //con.Open();
                        cmd3.Parameters.AddWithValue("@借用人代號", str);
                        cmd3.Parameters.AddWithValue("@借用人代號2", txt借用人代號.Text);
                        cmd3.Parameters.AddWithValue("@姓名", txt借用人姓名.Text);
                        cmd3.Parameters.AddWithValue("@備註", rtb借用人備註.Text.ToString());

                        cmd3.ExecuteNonQuery();
                        MessageBox.Show("更新成功");
                        LoadAll借用人();
                        ResetText借用人();
                        con.Close();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt借用人代號.Text.ToString() + "' 不由來");
                        con.Close();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LoadAll借用人();
                Close();
                return;
            }            
        }

        private void btnDelete借用人_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                if (txt借用人代號.Text == "") { MessageBox.Show("請您輸入借用人代號刪除"); }
                else
                {
                   
                    DialogResult answer = MessageBox.Show("您確定要出入這個產品碼", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    { 
                        con.Open();
                        string sp_delete = "sp_圖面借用人_刪除";
                        //string sp_delete = "delete from 圖面借用人 where 借用人代號='" + txt借用人代號.Text + "'";
                        SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                        //SqlCommand sqldelete = new SqlCommand();
                        //sqldelete.Connection = con;
                        sqldelete.CommandType = CommandType.StoredProcedure;
                        //sqldelete.CommandText = sp_delete;
                        sqldelete.Parameters.AddWithValue("@借用人代號", txt借用人代號.Text);
                        MessageBox.Show("刪除成功");
                        sqldelete.ExecuteNonQuery();
                        con.Close();
                        LoadAll借用人();
                        ResetText借用人();
                        return;
                    }
                    else
                    {
                        LoadAll借用人();
                        return;
                    }
                    
                }
            }
            catch (Exception ex)
            {
                con.Close();
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void txt建檔人1_TextChanged(object sender, EventArgs e)
        {
            //MessageBox.Show(txt建檔人1.Text);
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 圖面建檔人 where 建檔人代號=N'{0}'", txt建檔人1.Text);
                SqlCommand cmd = new SqlCommand(strcmd,con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read())temp= x[1].ToString();
                if (temp == "") lbName建檔人.Text = "";
                else lbName建檔人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt借用人1_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 圖面借用人 where 借用人代號=N'{0}'", txt借用人1.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName借用人.Text = "";
                else lbName借用人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt廠區1_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 圖面廠區 where 廠區代號=N'{0}'", txt廠區1.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName廠區.Text = "";
                else lbName廠區.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt製造商1_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 圖面製造商 where 製造商代號=N'{0}'", txt製造商1.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName製造商.Text = "";
                else lbName製造商.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        //End Code 借用人
        //==========================================================================================================//
    }
}
